package com.riven.bilibilidemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.ZhiBoActivity;
import com.riven.bilibilidemo.activity.Zhibo_Banner_connect;
import com.riven.bilibilidemo.bean.BeanList;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by S on 2017/3/13.
 */

public class MyRecyclerViewAdapter  extends RecyclerView.Adapter{
    //直播页Banner
    private static final int TYPE_BANNER = 1;
    //直播分类Title
    private static final int TYPE_PARTITION = 2;
    private final Context context;
    private final BeanList.DatashouyeBean dataList;
    List<String> listImg = new ArrayList<String>();
    private List<BeanList.DatashouyeBean.BannershouyeBean> bannerList;
    private List<BeanList.DatashouyeBean.PartitionsshouyeBean> beanList;
    private MyViewHolder1 holder1;

    public MyRecyclerViewAdapter(Context context, BeanList.DatashouyeBean dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;

        switch (viewType){
            case TYPE_BANNER:
                view = View.inflate(context, R.layout.zhibo_banner_recycler, null);
                holder = new MyViewHolder1(view);
                break;
            case TYPE_PARTITION:
                view = View.inflate(context, R.layout.zhibo_recycler, null);
                holder = new MyViewHolder2(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        bannerList = dataList.getBanner();
        beanList = dataList.getPartitions();
        switch (getItemViewType(position)){
            case TYPE_BANNER:
                holder1 = (MyViewHolder1) holder;

                for (int i = 0;i<bannerList.size();i++){
                    String img = bannerList.get(i).getImg();
                    listImg.add(img);
                }
                Banneradd(listImg, holder1.zhibo_banner);
                bannerOnClick();
            break;
            case TYPE_PARTITION:
                final MyViewHolder2 holder2 = (MyViewHolder2) holder;
                Glide.with(context).load(beanList.get(position).getPartition().getSub_icon().getSrc()).into(holder2.img_item);
                holder2.text_item.setText(beanList.get(position).getPartition().getName());
                holder2.count_item.setText(beanList.get(position).getPartition().getCount()+"");
                holder2.recyclercardView.setLayoutManager(new GridLayoutManager(context,2));
                List<BeanList.DatashouyeBean.PartitionsshouyeBean.LivesshouyeBeanX> lives = beanList.get(position).getLives();
                holder2.recyclercardView.setAdapter(new MyTwoRecyclerView(context,lives));
                break;
        }

    }
//点击banner
    private void bannerOnClick() {
        holder1.zhibo_banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(context, Zhibo_Banner_connect.class);
                intent.putExtra("url",bannerList.get(position).getLink());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return TYPE_BANNER;
        }else{
            return TYPE_PARTITION;
        }

    }

    @Override
    public int getItemCount() {

            return 9;

    }

    private void Banneradd(List<String> bannerList,Banner banner) {
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(bannerList);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }
    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);

        }

    }
    class MyViewHolder1 extends RecyclerView.ViewHolder{

        private final Banner zhibo_banner;

        public MyViewHolder1(View itemView) {
            super(itemView);
            zhibo_banner = (Banner) itemView.findViewById(R.id.zhibo_banner);
        }
    }
    class MyViewHolder2 extends RecyclerView.ViewHolder{

        private final ImageView img_item;
        private final TextView text_item;
        private final TextView count_item;
        private final RecyclerView recyclercardView;

        public MyViewHolder2(View itemView) {
            super(itemView);
            img_item = (ImageView) itemView.findViewById(R.id.zhibo_img_item);
            text_item = (TextView) itemView.findViewById(R.id.zhibo_text_item);
            count_item = (TextView) itemView.findViewById(R.id.zhibo_count_item);
            recyclercardView = (RecyclerView) itemView.findViewById(R.id.recyclerCardView);
        }
    }
}
